import { FormProps } from 'antd';
import './index.scss'
import { cancelOrderApi } from '@/api/order';

type Prop = {
  orderId: number | undefined,
  onSuccess:Function
}
export const CancelOrder = memo(forwardRef((prop: Prop, ref) => {
  const { orderId,onSuccess } = prop
  const { Option } = Select;
  // 添加和修改员工
  const [open, setOpen] = useState(false);
  const showModal = () => {
    setOpen(true)
  }
  const handleCancel = () => {
    setOpen(false);
  };
  type FieldType = {
    cancelReason?: string;
  };

  const onFinish: FormProps<FieldType>['onFinish'] = async (values) => {
    await cancelOrderApi({
      cancelReason: values.cancelReason,
      id: orderId
    })
    setOpen(false);
    onSuccess()
  };
  useImperativeHandle(
    ref,
    () => {
      return {
        showModal,
      }
    }
  );
  return (
    <Modal
      title="取消订单"
      centered
      open={open}
      onCancel={handleCancel}
      footer={null}
    >
      <Form
        name="catetroy"
        layout="inline"
        labelCol={{ span: 4 }}
        onFinish={onFinish}
        style={{ minHeight: 80 }}
        autoComplete="off"
      >
        <Form.Item<FieldType>
          style={{ width: '100%' }}
          name="cancelReason" label="取消原因"
          rules={[{ required: true, message: '请选择取消原因!' }]}>
          <Select
            placeholder="请选择取消原因"
            allowClear
          >
            <Option value="订单量较多，暂时无法接单">订单量较多，暂时无法接单</Option>
            <Option value="菜品已销售完，暂时无法接单">菜品已销售完，暂时无法接单</Option>
            <Option value="骑手不足无法配送">骑手不足无法配送</Option>
            <Option value="客户电话取消">客户电话取消</Option>
          </Select>
        </Form.Item>
        <Form.Item style={{ width: '100%' }}>
          <div className="flex flex-justify-end">
            <Button type="default" className="mr-4" onClick={handleCancel}>
              取消
            </Button>
            <Button type="primary" htmlType="submit">
              确认
            </Button>
          </div>
        </Form.Item>
      </Form>
    </Modal>
  )
}))